<template>
  <div style="height: 100vh;display: flex;align-items: center;justify-content: center;background-color: #0f9876">
    <div style="display: flex;background-color: white;width: 50%;border-radius: 5px;overflow: hidden">
      <div style="flex: 1;">
        <img src="@/assets/css/login.png" style="width: 100%">
      </div>
      <div style="flex: 1;display: flex;align-items: center;justify-content: center">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="loginRef">
          <div style="font-size: 20px;font-weight: bold;text-align: center;margin-bottom: 20px">
            欢迎登录智能仓库管理系统
          </div>
          <el-form-item prop="no">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号"
                      v-model="user.no"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex">
              <el-input prefix-icon="el-icon-circle-check" size="medium" style="flex: 1" placeholder="请输入验证码"
                        v-model="user.code">
              </el-input>
              <div style="flex: 1;height: 36px">
                <valid-code @update:value="getCode"></valid-code>
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login" >登 录</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">还没有账号? 请 <span style="color: #409EFF;cursor: pointer"  @click="$router.push('/register')" >注册</span></div>
<!--            <div style="flex: 1; text-align: right"><span style="color: #409EFF;cursor: pointer">忘记密码?</span></div>-->
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

import ValidCode from "@/components/ValidCode.vue";
import axios from "axios";
import {login} from "@/api/house";
export default {
  name: 'Login1',
  components: {
    ValidCode
  },
  data() {
    //校验验证码
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'));
      }
      else if (value.toLowerCase() !== this.code) {
        callback(new Error('验证码错误'));
      }
      else {
        callback();
      }
    };
    return {
      code: '',//验证码组件传过来得code
      rules: {
        no: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        code: [
          {validator: validateCode, trigger: 'blur'}
        ],
      },
      user: {
        code: '',//表单里用户输入得code验证码
        no: '',
        password: ''
      }

    }
  },
  methods: {
    // login() {
    //   this.$refs.loginRef.validate((valid) => {
    //     if (valid) {
    //       //验证通过
    //       //去后台验证用户名密码
    //       this.$axios.post(this.$httpUrl + '/user/login', this.user).then(res => res.data).then(res => {
    //         console.log(res)
    //         if (res.code == 200) {
    //           //存储
    //           sessionStorage.setItem("CurUser", JSON.stringify(res.data.user))
    //           this.$store.commit("setMenu", res.data.menu)
    //           localStorage.setItem("Token",JSON.stringify(res.data.token))
    //           //跳转到主页
    //           this.$router.replace('/Index')
    //           this.$message.success("登录成功")
    //         } else {
    //           alert('登录失败，用户名或密码错误！')
    //           return false;
    //         }
    //       })
    //     } else {
    //       console.log('校验失败');
    //       return false;
    //     }
    //
    //     console.log(this.code)
    //   })
    // },

    login(){
      this.$refs.loginRef.validate((valid) => {
        if (valid) {
          login(this.user).then(res => res.data).then(res => {
            if (res.code == 200) {
              //存储
              sessionStorage.setItem("CurUser", JSON.stringify(res.data.user))
              this.$store.commit("setMenu", res.data.menu)
              localStorage.setItem("Token", JSON.stringify(res.data.token))
              //跳转到主页
              this.$router.replace('/Index')
              this.$message.success("登录成功")
            } else {
              alert('登录失败，用户名或密码错误！')
              return false;
            }
          })
        } else {
          console.log('校验失败');
          return false;
        }
      })
    },

    getCode(code) {
      console.log(code)
      this.code = code.toLowerCase()
    }
  }
}
</script>
<style scoped>

</style>